#include <RF24.h>
#include <RF24_config.h>
#include <nRF24L01.h>
#include <printf.h>

// 包含库
//-------------------------------------------------------------------------------------------------------------
#include "NRF.h"
#include "Keys.h"
#include "Screen.h"
#include "Bluetooth.h"
#include "WIFI.h"
#include "LED.h"
#include "Buzzer.h"
#include "controller_keys.h" // 按键引脚定义
#include "chinese_32.h"  // 中文字库

// 包含图标：一级菜单
#include "icons/1nrf.h"
#include "icons/2game.h"
#include "icons/3vs.h"
#include "icons/4info.h"
#include "icons/5ble.h"
#include "icons/6set.h"

// 包含图标：二级菜单
#include "icons/1nrf/1pickup.h"
/*
#include "icons/1nrf/2truck.h"
#include "icons/1nrf/3tank.h"
#include "icons/1nrf/4drone.h"
#include "icons/1nrf/5excavator.h"
#include "icons/1nrf/6ship.h"
*/
#include "icons/2game/2_1snake.h"
#include "icons/2game/2_2brick.h"
#include "icons/2game/2_3plane.h"
#include "icons/2game/2_4num2048.h"
#include "icons/2game/2_5tetris.h"
#include "icons/3vs/3_1_ball.h"
#include "icons/4info/4_1_bilibili.h"
#include "icons/4info/4_2_weather.h"
#include "icons/4info/4_3_stock.h"
#include "icons/6set/6_1_keysTest.h"
#include "icons/6set/6_2_cube.h"

//自定义图标
#include "icons/battery.h"

// 全局变量，烧录前确认！
//-------------------------------------------------------------------------------------------------------------
int ID = 0;		  // 遥控器ID  0:黑 1:白(默认0)
bool show_CUP_logo = 0; //显示中石油北京logo

// 实例化对象
//-------------------------------------------------------------------------------------------------------------
NRF nrf;		 // 通信模块
Keys keys;	     // 按键
Screen screen;   // 屏幕
Bluetooth bt;	 // 蓝牙
WIFI wifi;	     // WiFi
LED led;		 // 板载LED
Buzzer buzzer;   // 蜂鸣器



// 函数声明
//-------------------------------------------- 一级菜单  -------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
void menu();			    // 0.主菜单     ->   1.NRF遥控  2.蓝牙手柄  3.系统设置  4.网络信息
void NRFControl();		// 1.NRF遥控    ->   1.皮卡 2.货车 3.坦克 4.无人机 5.挖掘机 6.舰船
//void localGame();		  // 2.蓝牙手柄   ->   1.贪吃蛇 2.打砖块 3.飞机大战 4.2048 5.俄罗斯方块
//void VSGame();			  // 3.双人对战   ->   1.弹球
void btGamepad();		  // 2.蓝牙手柄
void netInfo();			  // 3.网络信息   ->   1.哔哩哔哩 2.天气预报 3.股票基金
void systemSet();		  // 4.系统设置   ->   1.按键测试 2.陀螺仪立方体


//--------------------------------------------- 二级菜单 -------------------------------------------------------
//---------------------------------------------1.NRF遥控--------------------------------------------------------
void pickup();			// 1.1 皮卡
void nrf_rc( uint8_t addr, uint8_t ch);

//----------------------------------------------2.蓝牙手柄------------------------------------------------------

//----------------------------------------------3.网络信息------------------------------------------------------
void bilibili();		// 4.1 哔哩哔哩
void weather();			// 4.2 天气预报
void stock();			// 4.3 股票基金

//----------------------------------------------4.系统设置------------------------------------------------------
void keysTest();		// 6.1 按键测试
void battery_status();			// 6.2 陀螺仪立方体

//----------------------------------------------工具方法------------------------------------------------------
int getVolADC();					// 立刻获取电压ADC值
int getVol();						// 立刻获取电压值（mV）
int setGetVolTimer(int time);		// 设置周期性获取电压值（时间ms），返回定时器的ID
void closeGetVolTimer(int timerID); // 关闭周期性获取电压（定时器ID）



void setup() {
	Serial.begin(115200);

	nrf.init(ID, 0);			 // (遥控器ID, 通信功率，0-3)
	keys.init(ID);
	screen.init();
	buzzer.init();
	led.init();
  
  //设置衰减，读电压用
  analogSetPinAttenuation( PIN_VOL, ADC_11db);

	// 基本功能测试
	{
		//nrf.testConToCon();		    // 连遥控器间的测试连接 √
		//keysTest();				        // 按键测试,显示kvs到小屏幕上 √
		//keys.dounnceTest();		    // 按键消抖测试 √
	}
  screen.spr.loadFont(chinese_32);   // 加载自定义中文字库
  if(show_CUP_logo){ screen.hello_CUP();  }

  pickup();
	//menu();									// 主菜单
}


void loop() {
	//con.keys.kvs_update();         // √
	//con.keys.ShowInSerial();		 // √	
	delay(1000);
}



// ----------------------------------------- 以下是功能的具体实现 -----------------------------------------------

//-------------------------------------------- 一级菜单  -------------------------------------------------------
// 0.主菜单     ->   1.NRF遥控  2.本机游戏  3.双人对战  4.网络信息  5.蓝牙手柄  6.系统设置
void menu()
{
	bool pushFlag = true;	// 需要刷新屏幕的标志
	int menuIndex = 0;		// 菜单索引
	String menuName[4] = { "NRF遥控", "蓝牙手柄", "网络信息", "系统设置" };	// 菜单名字

	screen.spr.setTextColor(TFT_WHITE);		// 设置字体颜色
	screen.spr.setTextDatum(TC_DATUM);		// 设置字体对齐方式 TC_DATUM:居中对齐(基准坐标为上方的中点)

	while (true)
	{
		// 左键菜单索引-1，右键+1。并把刷新标志改为真
		if (keys.left.pressed())
		{
			menuIndex--;
			pushFlag = true;
			if (menuIndex < 0) { menuIndex = 3; }
		}
		else if (keys.right.pressed())
		{
			menuIndex++;
			pushFlag = true;
			if (menuIndex > 3) { menuIndex = 0; }
		}

		// 当刷新标准为true时，刷新屏幕显示。完事把标记改为假
		if (pushFlag)
		{
			//screen.spr.fillSprite(TFT_BLACK);		// 清屏
			screen.spr.fillRect(0, 145, 240, 40, TFT_BLACK);    // 把字体区域清空
			screen.spr.drawString(menuName[menuIndex], 120, 150, 4);	// 显示菜单名字

      /* 六个紫色点的绘制 */
			//screen.spr.fillSmoothRoundRect(20, 450, 20, 12, 3, TFT_VIOLET, TFT_BLACK);
			screen.spr.fillSmoothRoundRect(56, 450, 20, 12, 3, TFT_VIOLET, TFT_BLACK);
			screen.spr.fillSmoothRoundRect(92, 450, 20, 12, 3, TFT_VIOLET, TFT_BLACK);
			screen.spr.fillSmoothRoundRect(128, 450, 20, 12, 3, TFT_VIOLET, TFT_BLACK);
			screen.spr.fillSmoothRoundRect(164, 450, 20, 12, 3, TFT_VIOLET, TFT_BLACK);
			//screen.spr.fillSmoothRoundRect(200, 450, 20, 12, 3, TFT_VIOLET, TFT_BLACK);

      /* 根据当前菜单选择决定展示的图片和涂蓝的点 */
			if (menuIndex == 0) { screen.spr.pushImage(20, 220, 200, 200, image_data_1nrf);  screen.spr.fillSmoothRoundRect(56, 450, 20, 12, 3, TFT_CYAN, TFT_BLACK); }
			if (menuIndex == 1) { screen.spr.pushImage(20, 220, 200, 200, image_data_5ble); screen.spr.fillSmoothRoundRect(92, 450, 20, 12, 3, TFT_CYAN, TFT_BLACK); }
			if (menuIndex == 2) { screen.spr.pushImage(20, 220, 200, 200, image_data_4info);   screen.spr.fillSmoothRoundRect(128, 450, 20, 12, 3, TFT_CYAN, TFT_BLACK); }
			if (menuIndex == 3) { screen.spr.pushImage(20, 220, 200, 200, image_data_6set); screen.spr.fillSmoothRoundRect(164, 450, 20, 12, 3, TFT_CYAN, TFT_BLACK); }
      /*
			if (menuIndex == 4) { screen.spr.pushImage(20, 220, 200, 200, image_data_5ble);  screen.spr.fillSmoothRoundRect(164, 450, 20, 12, 3, TFT_CYAN, TFT_BLACK); }
			if (menuIndex == 5) { screen.spr.pushImage(20, 220, 200, 200, image_data_6set);  screen.spr.fillSmoothRoundRect(200, 450, 20, 12, 3, TFT_CYAN, TFT_BLACK); }
      */

			//Serial.println("push"); //用于验证按一下键才刷新屏幕，而不是一直刷新
			lcd_PushColors(0, 0, screen.spr.width(), screen.spr.height(), (uint16_t*)screen.spr.getPointer());	// 将数据显示到屏幕上
			pushFlag = false;
		}

		// 按下o键，进入二级菜单
		if (keys.o.pressed())
		{
			// 基于menuIndex进入不同的二级菜单
			switch (menuIndex)
			{
			case 0:
				NRFControl();		// 进入NRF遥控菜单
				pushFlag = true;	// 退出二级菜单后，需要刷新主菜单的屏幕
				break;
			case 1:
				btGamepad();		// 进入蓝牙手柄菜单
				pushFlag = true;
				break;
			case 2:
				netInfo();			// 进入双人对战菜单
				pushFlag = true;
				break;
			case 3:
				systemSet();			// 进入网络信息菜单
				pushFlag = true;
				break;

			default:
				break;
			}

		}

		delay(10);
	}
}

// 1.NRF遥控    ->   1.皮卡 2.货车 3.坦克 4.无人机 5.挖掘机 6.舰船
void NRFControl()
{
  screen.spr.fillSprite(TFT_BLACK);

	bool pushFlag = true;	        // 需要刷新屏幕的标志
	uint8_t nrfIndex = 0;		      // 修改位数索引
  uint8_t address_set = 23, ch_set = 11;  // nrf遥控器设置
  int add_shi = 2, add_ge = 3 , ch_bai = 0, ch_shi = 1, ch_ge = 1;
  char real_add[6] = "spr23";

  /* 框架绘制 */
  screen.spr.setTextColor(TFT_WHITE);		// 设置字体颜色
	screen.spr.setTextDatum(TC_DATUM);		// 设置字体对齐方式 TC_DATUM:居中对齐(基准坐标为上方的中点)

  screen.spr.drawCentreString("遥控设置", 120, 40, 12);
  screen.spr.drawCentreString("ADDR:", 120, 120, 8);
  screen.spr.drawCentreString("SPRXX", 120, 160, 8);
  screen.spr.drawCentreString("CH:", 120, 240, 8);
  screen.spr.drawCentreString("XXX", 120, 280, 8);

  screen.spr.drawCentreString("Confirm: O  ", 120, 420, 6);
  screen.spr.drawCentreString("QUIT: Rboard", 120, 460, 6);
  lcd_PushColors(0, 0, 240, 536, (uint16_t*)screen.spr.getPointer());   // 改方向后这里长款也要同步改

	while (true)
	{
    keys.kvs_update();
    //左右键决定修改哪一位数字
		if (keys.left.pressed())
		{
			nrfIndex--;
			pushFlag = true;
			if (nrfIndex < 0) { nrfIndex = 4; }
		}
		else if (keys.right.pressed())
		{
			nrfIndex++;
			pushFlag = true;
			if (nrfIndex > 4) { nrfIndex = 0; }
		}
    //上下键修改具体值
    if (keys.up.pressed())
		{
			switch(nrfIndex)
      {
        case 0: //地址的第四位
          add_shi += 1;
          if (add_shi > 9) { add_shi = 0; }
          break;
        case 1:
          add_ge += 1;
          if (add_ge > 9) { add_ge = 0; }
          break;

        default:
				  break;

      }
      address_set = add_shi * 10 + add_ge;
			pushFlag = true;
			
		}
		else if (keys.down.pressed())
		{

		}

    //绘制选择修改的位置
		if (pushFlag)
		{
      //绘制选择修改的圈圈
      screen.spr.fillRect(0, 190, 240, 20, TFT_BLACK);
      screen.spr.fillRect(0, 310, 240, 20, TFT_BLACK);
      if(nrfIndex == 0) screen.spr.drawSmoothCircle( 140, 200, 8, TFT_VIOLET, TFT_BLUE);
      if(nrfIndex == 1) screen.spr.drawSmoothCircle( 165, 200, 8, TFT_VIOLET, TFT_BLUE);
      if(nrfIndex == 2) screen.spr.drawSmoothCircle( 95, 320, 8, TFT_VIOLET, TFT_BLUE);
      if(nrfIndex == 3) screen.spr.drawSmoothCircle( 120, 320, 8, TFT_VIOLET, TFT_BLUE);
      if(nrfIndex == 4) screen.spr.drawSmoothCircle( 145, 320, 8, TFT_VIOLET, TFT_BLUE);

      //绘制地址
      screen.spr.fillRect(0, 160, 240, 40, TFT_BLACK);    // 把字体区域清空0
      real_add[3] = address_set / 10 + 48;
      real_add[4] = address_set % 10 + 48;
      screen.spr.drawCentreString((String)real_add, 120, 160, 8);

			//screen.spr.fillSprite(TFT_BLACK);		// 清屏
			//screen.spr.fillRect(0, 145, 240, 40, TFT_BLACK);    // 把字体区域清空
			//screen.spr.fillRect(0, 440, 240, 25, TFT_BLACK);

      /*
			screen.spr.fillSmoothCircle(30,  456, 6, TFT_VIOLET, TFT_BLACK);
			screen.spr.fillSmoothCircle(66,  456, 6, TFT_VIOLET, TFT_BLACK);
			screen.spr.fillSmoothCircle(102, 456, 6, TFT_VIOLET, TFT_BLACK);
			screen.spr.fillSmoothCircle(138, 456, 6, TFT_VIOLET, TFT_BLACK);
			screen.spr.fillSmoothCircle(174, 456, 6, TFT_VIOLET, TFT_BLACK);
			screen.spr.fillSmoothCircle(210, 456, 6, TFT_VIOLET, TFT_BLACK);

      */

			//Serial.println("push"); //用于验证按一下键才刷新屏幕，而不是一直刷新
			lcd_PushColors(0, 0, screen.spr.width(), screen.spr.height(), (uint16_t*)screen.spr.getPointer());	// 将数据显示到屏幕上
			pushFlag = false;
		}

		if (keys.o.pressed())
		{
      //进遥控界面
			nrf_rc( address_set, address_set);
		}
		else if (keys.board_R.pressed())
		{
			screen.spr.fillSprite(TFT_BLACK);
			Serial.println("返回主菜单");
			break;
		}


		delay(10);
	}
}

// 4.网络信息   ->   1.哔哩哔哩 2.天气预报 3.股票基金
void netInfo()
{
	bool infoFlag = true;
	int infoIndex = 0;
	String infoName[3] = { "哔哩哔哩", "天气预报", "股票基金" };

	screen.spr.setTextColor(TFT_WHITE);		// 设置字体颜色
	screen.spr.setTextDatum(TC_DATUM);		// 设置字体对齐方式 TC_DATUM:居中对齐(基准坐标为上方的中点)

	while (true)
	{
		if (keys.left.pressed())
		{
			infoIndex--;
			infoFlag = true;
			if (infoIndex < 0) { infoIndex = 2; }
		}
		else if (keys.right.pressed())
		{
			infoIndex++;
			infoFlag = true;
			if (infoIndex > 2) { infoIndex = 0; }
		}

		if (infoFlag)
		{
			//screen.spr.fillSprite(TFT_BLACK);		// 清屏
			screen.spr.fillRect(0, 145, 240, 40, TFT_BLACK);    // 把字体区域清空
			screen.spr.fillRect(0, 440, 240, 25, TFT_BLACK);
			screen.spr.drawString(infoName[infoIndex], 120, 150, 4);	// 显示菜单名字

			screen.spr.fillSmoothCircle(84, 456, 6, TFT_VIOLET, TFT_BLACK);
			screen.spr.fillSmoothCircle(120, 456, 6, TFT_VIOLET, TFT_BLACK);
			screen.spr.fillSmoothCircle(156, 456, 6, TFT_VIOLET, TFT_BLACK);

			if (infoIndex == 0) { 
				screen.spr.fillRect(0, 400, 240, 20, TFT_BLACK);  
				screen.spr.pushImage(20, 220, 200, 181, image_data_4_1_bilibili); 
				screen.spr.fillSmoothCircle(84, 456, 6, TFT_CYAN, TFT_BLACK);
			}
			if (infoIndex == 1) { screen.spr.pushImage(20, 220, 200, 200, image_data_4_2_weather); screen.spr.fillSmoothCircle(120, 456, 6, TFT_CYAN, TFT_BLACK); }
			if (infoIndex == 2) { screen.spr.pushImage(20, 220, 200, 200, image_data_4_3_stock); screen.spr.fillSmoothCircle(156, 456, 6, TFT_CYAN, TFT_BLACK);}

			lcd_PushColors(0, 0, screen.spr.width(), screen.spr.height(), (uint16_t*)screen.spr.getPointer());	// 将数据显示到屏幕上
			infoFlag = false;
		}

		if (keys.o.pressed())
		{
			switch (infoIndex)
			{
			case 0:
				bilibili();
				infoFlag = true;
				break;
			case 1:
				weather();
				infoFlag = true;
				break;
			case 2:
				stock();
				infoFlag = true;
				break;
			default:
				break;
			}

		}
		else if (keys.x.pressed())
		{
			screen.spr.fillRect(0, 440, 240, 25, TFT_BLACK);
			Serial.println("返回主菜单");
			break;
		}


		delay(10);
	}
}

// 5.蓝牙手柄
void btGamepad() 
{
  //保留

}

// 6.系统设置   ->   1.按键测试 2.陀螺仪立方体
void systemSet()
{
	bool setFlag = true;
	int setIndex = 0;
	String setName[2] = { "按键测试", "电池电量" };

	screen.spr.setTextColor(TFT_WHITE);		// 设置字体颜色
	screen.spr.setTextDatum(TC_DATUM);		// 设置字体对齐方式 TC_DATUM:居中对齐(基准坐标为上方的中点)

	while (true)
	{
		if (keys.left.pressed())
		{
			setIndex--;
			setFlag = true;
			if (setIndex < 0) { setIndex = 1; }
		}
		else if (keys.right.pressed())
		{
			setIndex++;
			setFlag = true;
			if (setIndex > 1) { setIndex = 0; }
		}

		if (setFlag)
		{
			//screen.spr.fillSprite(TFT_BLACK);		// 清屏
			screen.spr.fillRect(0, 145, 240, 40, TFT_BLACK);    // 把字体区域清空
			screen.spr.fillRect(0, 440, 240, 25, TFT_BLACK);
			screen.spr.drawString(setName[setIndex], 120, 150, 4);	// 显示菜单名字

			screen.spr.fillSmoothCircle(102, 456, 6, TFT_VIOLET, TFT_BLACK);
			screen.spr.fillSmoothCircle(138, 456, 6, TFT_VIOLET, TFT_BLACK);

			if (setIndex == 0) { screen.spr.pushImage(20, 220, 200, 200, image_data_6_1_keysTest); screen.spr.fillSmoothCircle(102, 456, 6, TFT_CYAN, TFT_BLACK);}
			if (setIndex == 1) { screen.spr.pushImage(20, 220, 200, 200, image_data_battery);  screen.spr.fillSmoothCircle(138, 456, 6, TFT_CYAN, TFT_BLACK);}

			lcd_PushColors(0, 0, screen.spr.width(), screen.spr.height(), (uint16_t*)screen.spr.getPointer());	// 将数据显示到屏幕上
			setFlag = false;
		}

		if (keys.o.pressed())
		{
			switch (setIndex)
			{
			case 0:
				keysTest();
				setFlag = true;
				break;
			case 1:
				battery_status();
				setFlag = true;
				break;
			default:
				break;
			}

		}
		else if (keys.x.pressed())
		{
			screen.spr.fillRect(0, 440, 240, 25, TFT_BLACK);
			Serial.println("返回主菜单");
			break;
		}


		delay(10);
	}
}



//--------------------------------------------- 二级菜单 -------------------------------------------------------
//--------------------------------------------- 1.NRF遥控 -------------------------------------------------------
// 1.1 皮卡	
void pickup() 
{

  const byte address_pickup[6] = "spr23";      //地址

  //NRF24模块
  nrf.radio.begin();
  nrf.radio.setChannel(11);

	nrf.radio.openWritingPipe(address_pickup);  // 发送端打开通信通道
	nrf.radio.setPALevel(RF24_PA_HIGH);         // 设置通信功率，参数可选：RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX
	nrf.radio.stopListening();                  // 停止监听，即设置为发送端

  //屏幕
	screen.spr.fillSprite(TFT_BLACK);
	screen.spr.setTextDatum(TL_DATUM);   // 字体对齐方式 TL_DATUM:左上对齐(基准坐标为左上角)

  //14字节待发送数据
  uint8_t data_send[14];

  while (true)
	{
    //数据更新
		keys.kvs_update();

    //数据写入
    data_send[0] = 0xFF; data_send[1] = 0xFF; data_send[12] = 0xAA; data_send[13] = 0xAA;
    data_send[2] = ~keys.shoulder_paddle_calc();
    data_send[3] = ~keys.key_press_calc();
    data_send[4] = (uint8_t)keys.kvs.LX;        //转换后发送，接收后转换
    data_send[5] = (uint8_t)keys.kvs.LY;
    data_send[6] = (uint8_t)keys.kvs.RX;
    data_send[7] = (uint8_t)keys.kvs.RY;
    data_send[8] = (uint8_t)keys.kvs.L_knob;
    data_send[9] = (uint8_t)keys.kvs.R_knob;
    data_send[10] = (uint8_t)keys.kvs.angleX;
    data_send[11] = (uint8_t)keys.kvs.angleY;

		//发送
		nrf.radio.write(&data_send, sizeof(data_send));
		delay(10);

		screen.spr.fillSprite(TFT_BLACK);
		screen.spr.drawString("LX: " + String(keys.kvs.LX), 30, 20);
		screen.spr.drawString("LY: " + String(keys.kvs.LY), 30, 60);
		screen.spr.drawString("RX: " + String(keys.kvs.RX), 30, 100);
		screen.spr.drawString("RY: " + String(keys.kvs.RY), 30, 140);

    screen.spr.drawString("LN: " + String(keys.kvs.L_knob), 30, 180);
		screen.spr.drawString("RN: " + String(keys.kvs.R_knob), 30, 220);

		//screen.spr.drawString("QUIT: Rboard", 30, 340);
    screen.spr.drawString("电池电压:"+ String( getVol()/500.0  ) , 30, 420);

		lcd_PushColors(0, 0, screen.spr.width(), screen.spr.height(), (uint16_t*)screen.spr.getPointer());	// 将数据显示到屏幕上
    
		if (keys.board_R.pressed()) { screen.spr.fillSprite(TFT_BLACK); screen.spr.setTextDatum(TC_DATUM); break; } //退出

	}

}

void nrf_rc( uint8_t addr, uint8_t ch)
{
  uint8_t address[6] = "spr23";
  address[3] = addr / 10 + 48;
  address[4] = addr % 10 + 48;
  //NRF24模块
  nrf.radio.begin();
  nrf.radio.setChannel(ch);

	nrf.radio.openWritingPipe(address);         // 发送端打开通信通道
	nrf.radio.setPALevel(RF24_PA_HIGH);         // 设置通信功率，参数可选：RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX
	nrf.radio.stopListening();                  // 停止监听，即设置为发送端

  //屏幕
	screen.spr.fillSprite(TFT_BLACK);
	screen.spr.setTextDatum(TL_DATUM);   // 字体对齐方式 TL_DATUM:左上对齐(基准坐标为左上角)

  //14字节待发送数据
  uint8_t data_send[14];

  while (true)
	{
    //数据更新
		keys.kvs_update();

    //数据写入
    data_send[0] = 0xFF; data_send[1] = 0xFF; data_send[12] = 0xAA; data_send[13] = 0xAA;
    data_send[2] = ~keys.shoulder_paddle_calc();
    data_send[3] = ~keys.key_press_calc();
    data_send[4] = (uint8_t)keys.kvs.LX;        //转换后发送，接收后转换
    data_send[5] = (uint8_t)keys.kvs.LY;
    data_send[6] = (uint8_t)keys.kvs.RX;
    data_send[7] = (uint8_t)keys.kvs.RY;
    data_send[8] = (uint8_t)keys.kvs.L_knob;
    data_send[9] = (uint8_t)keys.kvs.R_knob;
    data_send[10] = (uint8_t)keys.kvs.angleX;
    data_send[11] = (uint8_t)keys.kvs.angleY;

		//发送
		nrf.radio.write(&data_send, sizeof(data_send));
		delay(10);

		screen.spr.fillSprite(TFT_BLACK);
		screen.spr.drawString("LX: " + String(keys.kvs.LX), 30, 20);
		screen.spr.drawString("LY: " + String(keys.kvs.LY), 30, 60);
		screen.spr.drawString("RX: " + String(keys.kvs.RX), 30, 100);
		screen.spr.drawString("RY: " + String(keys.kvs.RY), 30, 140);

    screen.spr.drawString("LN: " + String(keys.kvs.L_knob), 30, 180);
		screen.spr.drawString("RN: " + String(keys.kvs.R_knob), 30, 220);

		screen.spr.drawString("QUIT: Rboard", 30, 340);
    screen.spr.drawString("电池电压:"+ String( getVol()/500.0  ) , 30, 420);

		lcd_PushColors(0, 0, screen.spr.width(), screen.spr.height(), (uint16_t*)screen.spr.getPointer());	// 将数据显示到屏幕上
    
		if (keys.board_R.pressed()) { screen.spr.fillSprite(TFT_BLACK); screen.spr.setTextDatum(TC_DATUM); break; } //退出

	}
}

//---------------------------------------------4.网络信息------------------------------------------------------
// 4.1 哔哩哔哩
void bilibili() { Serial.println("4.1 哔哩哔哩"); }

// 4.2 天气预报
void weather() { Serial.println("4.2 天气预报"); }

// 4.3 股票基金
void stock() { Serial.println("4.3 股票基金"); }


//---------------------------------------------5.蓝牙手柄------------------------------------------------------



//---------------------------------------------6.系统设置------------------------------------------------------
// 6.1 按键测试
void keysTest()
{
	// 按键测试的固定UI
	{
		// 清屏并绘制框架
		screen.spr.fillSprite(TFT_BLACK);
		screen.spr.drawString("按键测试", 120, 20, 4);

		// 4个前端按键
		screen.spr.drawSmoothCircle(35, 75, 15, TFT_GREEN, TFT_BLACK);  // 抗锯齿的圆形
		screen.spr.drawSmoothCircle(80, 70, 10, TFT_GREEN, TFT_BLACK);
		screen.spr.drawSmoothCircle(150, 70, 10, TFT_GREEN, TFT_BLACK);
		screen.spr.drawSmoothCircle(205, 75, 15, TFT_GREEN, TFT_BLACK);

		// 电位器旋钮
		screen.spr.drawRect(35, 110, 75, 20, TFT_CYAN);
		screen.spr.drawRect(130, 110, 75, 20, TFT_CYAN);

		// 4个拨杆开关
		screen.spr.drawRect(20, 150, 20, 30, TFT_PINK);
		screen.spr.drawRect(60, 150, 20, 30, TFT_PINK);
		screen.spr.drawRect(160, 150, 20, 30, TFT_PINK);
		screen.spr.drawRect(200, 150, 20, 30, TFT_PINK);

		// 摇杆
		screen.spr.drawRect(10, 200, 100, 100, TFT_CYAN);
		screen.spr.drawRect(130, 200, 100, 100, TFT_CYAN);

		// 板载按键
		screen.spr.drawRect(80, 320, 20, 10, TFT_WHITE);
		screen.spr.drawRect(140, 320, 20, 10, TFT_WHITE);

		// 功能键
		screen.spr.drawRect(40, 360, 20, 20, TFT_SILVER);
		screen.spr.drawRect(40, 400, 20, 20, TFT_SILVER);
		screen.spr.drawRect(10, 380, 20, 20, TFT_SILVER);
		screen.spr.drawRect(70, 380, 20, 20, TFT_SILVER);
		screen.spr.drawRect(180, 360, 20, 20, TFT_SILVER);
		screen.spr.drawRect(180, 400, 20, 20, TFT_SILVER);
		screen.spr.drawRect(150, 380, 20, 20, TFT_SILVER);
		screen.spr.drawRect(210, 380, 20, 20, TFT_SILVER);

		// 陀螺仪
		screen.spr.drawRect(75, 440, 90, 90, TFT_VIOLET);

		// 把内容推到屏幕显示。 改方向后这里长款也要同步改	
		lcd_PushColors(0, 0, 240, 536, (uint16_t*)screen.spr.getPointer());
	}

	// 实时显示按键状态
	while (true)
	{
		// 按键状态更新
		keys.kvs_update();

		// 前端4个按键
		keys.kvs.L_up == 0 ? screen.spr.fillSmoothCircle(35, 75, 12, TFT_GREEN, TFT_BLACK) : screen.spr.fillSmoothCircle(35, 75, 12, TFT_BLACK, TFT_BLACK);
		keys.kvs.L_down == 0 ? screen.spr.fillSmoothCircle(80, 70, 7, TFT_GREEN, TFT_BLACK) : screen.spr.fillSmoothCircle(80, 70, 7, TFT_BLACK, TFT_BLACK);
		keys.kvs.R_down == 0 ? screen.spr.fillSmoothCircle(150, 70, 7, TFT_GREEN, TFT_BLACK) : screen.spr.fillSmoothCircle(150, 70, 7, TFT_BLACK, TFT_BLACK);
		keys.kvs.R_up == 0 ? screen.spr.fillSmoothCircle(205, 75, 12, TFT_GREEN, TFT_BLACK) : screen.spr.fillSmoothCircle(205, 75, 12, TFT_BLACK, TFT_BLACK);

		// 电位旋钮
		screen.spr.fillRect(36, 111, 73, 18, TFT_BLACK);
		screen.spr.fillRect(131, 111, 73, 18, TFT_BLACK);
		screen.spr.fillRect(map(keys.kvs.L_knob, -100, 100, 37, 108) - 2, 111, 4, 18, TFT_CYAN);
		screen.spr.fillRect(map(keys.kvs.R_knob, -100, 100, 132, 203) - 2, 111, 4, 18, TFT_CYAN);

		// 4个拨杆开关
		if (keys.kvs.switch_L1 == 1)
		{
			screen.spr.fillRect(21, 151, 18, 13, TFT_PINK);
			screen.spr.fillRect(21, 166, 18, 13, TFT_BLACK);
			buzzer.on();
			led.on();
		}
		else
		{
			screen.spr.fillRect(21, 151, 18, 13, TFT_BLACK);
			screen.spr.fillRect(21, 166, 18, 13, TFT_PINK);
			buzzer.off();
			led.off();
		}

		if (keys.kvs.switch_L2 == 1)
		{
			screen.spr.fillRect(61, 151, 18, 13, TFT_PINK);
			screen.spr.fillRect(61, 166, 18, 13, TFT_BLACK);
		}
		else {
			screen.spr.fillRect(61, 151, 18, 13, TFT_BLACK);
			screen.spr.fillRect(61, 166, 18, 13, TFT_PINK);
		}

		if (keys.kvs.switch_R1 == 1) {
			screen.spr.fillRect(161, 151, 18, 13, TFT_PINK);
			screen.spr.fillRect(161, 166, 18, 13, TFT_BLACK);
		}
		else {
			screen.spr.fillRect(161, 151, 18, 13, TFT_BLACK);
			screen.spr.fillRect(161, 166, 18, 13, TFT_PINK);
		}

		if (keys.kvs.switch_R2 == 1) {
			screen.spr.fillRect(201, 151, 18, 13, TFT_PINK);
			screen.spr.fillRect(201, 166, 18, 13, TFT_BLACK);
		}
		else {
			screen.spr.fillRect(201, 151, 18, 13, TFT_BLACK);
			screen.spr.fillRect(201, 166, 18, 13, TFT_PINK);
		}

		// 摇杆
		screen.spr.fillRect(11, 201, 98, 98, TFT_BLACK);
		screen.spr.fillRect(131, 201, 98, 98, TFT_BLACK);
		screen.spr.fillSmoothCircle(10 + map(keys.kvs.LX, -100, 100, 10, 90), 200 + map(keys.kvs.LY, 100, -100, 10, 90), 8, TFT_CYAN, TFT_BLACK);
		screen.spr.fillSmoothCircle(130 + map(keys.kvs.RX, -100, 100, 10, 90), 200 + map(keys.kvs.RY, 100, -100, 10, 90), 8, TFT_CYAN, TFT_BLACK);

		// 板载按键
		keys.kvs.board_L == 0 ? screen.spr.fillRect(81, 321, 18, 8, TFT_WHITE) : screen.spr.fillRect(81, 321, 18, 8, TFT_BLACK);
		keys.kvs.board_R == 0 ? screen.spr.fillRect(141, 321, 18, 8, TFT_WHITE) : screen.spr.fillRect(141, 321, 18, 8, TFT_BLACK);

		// 功能按键
		keys.kvs.up == 0 ? screen.spr.fillRect(41, 361, 18, 18, TFT_SILVER) : screen.spr.fillRect(41, 361, 18, 18, TFT_BLACK);
		keys.kvs.down == 0 ? screen.spr.fillRect(41, 401, 18, 18, TFT_SILVER) : screen.spr.fillRect(41, 401, 18, 18, TFT_BLACK);
		keys.kvs.left == 0 ? screen.spr.fillRect(11, 381, 18, 18, TFT_SILVER) : screen.spr.fillRect(11, 381, 18, 18, TFT_BLACK);
		keys.kvs.right == 0 ? screen.spr.fillRect(71, 381, 18, 18, TFT_SILVER) : screen.spr.fillRect(71, 381, 18, 18, TFT_BLACK);
		keys.kvs.x == 0 ? screen.spr.fillRect(181, 361, 18, 18, TFT_SILVER) : screen.spr.fillRect(181, 361, 18, 18, TFT_BLACK);
		keys.kvs.a == 0 ? screen.spr.fillRect(181, 401, 18, 18, TFT_SILVER) : screen.spr.fillRect(181, 401, 18, 18, TFT_BLACK);
		keys.kvs.o == 0 ? screen.spr.fillRect(151, 381, 18, 18, TFT_SILVER) : screen.spr.fillRect(151, 381, 18, 18, TFT_BLACK);
		keys.kvs.b == 0 ? screen.spr.fillRect(211, 381, 18, 18, TFT_SILVER) : screen.spr.fillRect(211, 381, 18, 18, TFT_BLACK);

		// 陀螺仪
		int angleX = map(keys.kvs.angleX, -100, 100, 446, 524);
		int angleY = map(keys.kvs.angleY, -100, 100, 81, 159);
		screen.spr.fillRect(76, 441, 88, 88, TFT_BLACK);
		screen.spr.fillRect(angleY - 5, angleX - 5, 10, 10, TFT_VIOLET);

		// 把内容推到屏幕显示。 改方向后这里长款也要同步改
		lcd_PushColors(0, 0, 240, 536, (uint16_t*)screen.spr.getPointer());


		// 同时按下4个前端按键，退出本功能
		if (keys.b.pressed() && keys.x.pressed())
		{
			screen.spr.fillSprite(TFT_BLACK);									// 清屏
			lcd_PushColors(0, 0, 240, 536, (uint16_t*)screen.spr.getPointer());  // 把内容推到屏幕显示。 改方向后这里长款也要同步改
			break;
		}

		delay(1);
	}
}


// 6.2 电池状态展示
void battery_status() 
{ 
  while (true)
  {
    screen.battery_status();
    keys.kvs_update();
    // 按下板载右键，退出本功能
    if (keys.board_R.pressed()){ screen.spr.fillSprite(TFT_BLACK); screen.spr.setTextDatum(TC_DATUM); Serial.println("退出");break; } //退出

		delay(10);
  }
}

//--------------------------------------------- 工具方法 -------------------------------------------------------
// 获取电压ADC值
int getVolADC()
{
	return analogRead(PIN_VOL);
}

// 获取实际电压值（mV）
int getVol()
{
	return analogReadMilliVolts(PIN_VOL);
}

